import * as loginApi from '@/api/login'
import { defineStore } from 'pinia'
import { useAuthStore } from '@/store'
export const useUserStore = defineStore('user', {
  state: () => ({
    userInfo: null,
  }),
  getters: {
    userId() {
      return this.userInfo?.id
    },
    username() {
      return this.userInfo?.name
    },
    avatar() {
      return this.userInfo?.avatar
    },
  },
  actions: {
    setUser(user) {
      this.userInfo = user
    },
    resetUser() {
      this.$reset()
    },
    login(data) {
      const { setToken } = useAuthStore()
      return loginApi.login(data).then((res) => {
        setToken({ tokenName: res.data.tokenName, tokenValue: res.data.tokenValue })
      })
    },
  },
})
